package com.clp.util.math;

import com.clp.util.exception.EnumElemDoesNotExistException;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.math.BigDecimal;

@Getter
@AllArgsConstructor
public enum CompareOp {
    BIGGER(">", "大于") {
        @Override
        public boolean fit(BigDecimal leftVal, BigDecimal rightVal) {
            return leftVal.compareTo(rightVal) > 0;
        }
    },
    BIGGER_E(">=", "大于等于") {
        @Override
        public boolean fit(BigDecimal leftVal, BigDecimal rightVal) {
            return leftVal.compareTo(rightVal) >= 0;
        }
    },
    SMALLER("<", "小于") {
        @Override
        public boolean fit(BigDecimal leftVal, BigDecimal rightVal) {
            return leftVal.compareTo(rightVal) < 0;
        }
    },
    SMALLER_E("<=", "小于等于") {
        @Override
        public boolean fit(BigDecimal leftVal, BigDecimal rightVal) {
            return leftVal.compareTo(rightVal) <= 0;
        }
    },
    EQUAL("=", "等于") {
        @Override
        public boolean fit(BigDecimal leftVal, BigDecimal rightVal) {
            return leftVal.compareTo(rightVal) == 0;
        }
    },
    NOT_EQUAL("!=", "不等于") {
        @Override
        public boolean fit(BigDecimal leftVal, BigDecimal rightVal) {
            return leftVal.compareTo(rightVal) != 0;
        }
    };

    private final String expr;
    private final String desc;

    public static CompareOp gain(String expr) {
        for (CompareOp compareOp : CompareOp.values()) {
            if (compareOp.expr.equalsIgnoreCase(expr)) {
                return compareOp;
            }
        }
        throw new EnumElemDoesNotExistException(CompareOp.class);
    }

    public abstract boolean fit(BigDecimal leftVal, BigDecimal rightVal);
}
